import threading
import time
from tkinter import *

import bin_storage
import const
import file_mgmt


# import win32gui


class Window:

    def __init__(self, b2, fontsize, MF_channel):
        self.window = Toplevel()
        self.topmost = const.SIG_topmost_1
        self.window.wm_attributes('-topmost', self.topmost)
        title = 'f10'
        self.window.title(title)

        self.MF_channel = MF_channel
        self.b2 = b2
        self.labelfont = ('monospaced', fontsize, '')
        ##
        self.name = Label(self.window, text='', borderwidth=0, relief="groove")
        self.name.config(font=('raster', fontsize - 2))
        self.name.grid(row=0, column=0, padx=0, pady=0, columnspan=3)
        ##
        self.gong = Label(self.window, text='', borderwidth=2, relief="groove")
        self.gong.config(font=self.labelfont)
        self.gong.grid(row=1, column=0, padx=2, pady=2)
        self.wei = Label(self.window, text='', borderwidth=2, relief="groove")
        self.wei.config(font=self.labelfont)
        self.wei.grid(row=2, column=0, padx=2, pady=2)
        self.shareholder = Label(self.window, text='', borderwidth=2, relief="groove", fg='blue')
        self.shareholder.config(font=self.labelfont)
        self.shareholder.grid(row=3, column=0, padx=2, pady=2)
        self.special = Label(self.window, text='', borderwidth=2, relief="groove")
        self.special.config(font=self.labelfont)
        self.special.grid(row=4, column=0, padx=2, pady=2)
        ##
        self.gong1 = Label(self.window, text='', borderwidth=2, relief="groove")
        self.gong1.config(font=self.labelfont)
        self.gong1.grid(row=1, column=1, padx=2, pady=2)
        self.wei1 = Label(self.window, text='', borderwidth=2, relief="groove")
        self.wei1.config(font=self.labelfont)
        self.wei1.grid(row=2, column=1, padx=2, pady=2)
        self.shareholder1 = Label(self.window, text='', borderwidth=2, relief="groove", fg='blue')
        self.shareholder1.config(font=self.labelfont)
        self.shareholder1.grid(row=3, column=1, padx=2, pady=2)
        self.special1 = Label(self.window, text='', borderwidth=2, relief="groove")
        self.special1.config(font=self.labelfont)
        self.special1.grid(row=4, column=1, padx=2, pady=2)
        ##
        self.gong2 = Label(self.window, text='', borderwidth=2, relief="groove")
        self.gong2.config(font=self.labelfont)
        self.gong2.grid(row=1, column=2, padx=2, pady=2)
        self.wei2 = Label(self.window, text='', borderwidth=2, relief="groove")
        self.wei2.config(font=self.labelfont)
        self.wei2.grid(row=2, column=2, padx=2, pady=2)
        self.shareholder2 = Label(self.window, text='', borderwidth=2, relief="groove", fg='blue')
        self.shareholder2.config(font=self.labelfont)
        self.shareholder2.grid(row=3, column=2, padx=2, pady=2)
        self.special2 = Label(self.window, text='', borderwidth=2, relief="groove")
        self.special2.config(font=self.labelfont)
        self.special2.grid(row=4, column=2, padx=2, pady=2)
        ##
        self.labels = {'name': self.name, 'gong': [self.gong, self.gong1, self.gong2],
                       'wei': [self.wei, self.wei1, self.wei2],
                       'shareholder': [self.shareholder, self.shareholder1, self.shareholder2],
                       'special': [self.special, self.special1, self.special2]
                       }
        ##
        self.stock_name = ''
        self.stock_hwnd = 0
        self.window.protocol("WM_DELETE_WINDOW", self.on_closing)

        self.dic = bin_storage.Bin_storage().read(file_mgmt.push(dir='f10', ext="b")[1])

    def on_closing(self):
        self.b2.config(state=NORMAL)

        self.window.destroy()

    def update_text(self, text, name):
        self.labels['name']['text'] = name

        for i in text:
            for j in range(len(text[i])):

                self.labels[i][j]['text'] = text[i][j]
                for k in range(len(text[i]), 3):
                    self.labels[i][k]['text'] = ''

        self.window.update()




    def run_fn(self):
        selected_names = list(self.dic.keys())
        while True:
            name,sig = self.MF_channel

            if (name in selected_names) and (name != self.stock_name):
                self.stock_name = name
                self.update_text(self.dic[self.stock_name], self.stock_name)
            elif (name not in selected_names) and (name != self.stock_name):
                self.update_text('', name + ' not selected')

            if sig==const.SIG_EXIT:
                self.window.destroy()
            elif sig==const.SIG_topmost_1 or sig==const.SIG_topmost_0:
                if sig!=self.topmost:
                    self.window.wm_attributes('-topmost', sig)
                    self.topmost=sig
                    if sig==const.SIG_topmost_0:
                        self.window.lower()
            time.sleep(0.3)

    def run(self):
        t = threading.Thread(target=self.run_fn)
        t.start()


if __name__ == '__main__':
    w = Window(None, 16)
    t = threading.Thread(target=w.run_fn)
    t.start()
